%%   @author Nicolae Paladi <paladi@ituniv.se>
%%   @version 0.01 
%%   @title Test DB
%%   @doc db_create is the module which initially create the Mnesia tables

-module(company). 
-import(lists, [foreach/2]).
-compile(export_all).
-include("company.hrl").


%% The code below specifies where the Mnesia will be saved on the HDD
%%erl -mnesia dir '"/home/someplace/.."'

init() ->
	mnesia:create_table(employee,[{attributes,record_info(fields, employee)}]),
	mnesia:create_table(department,[{attributes,record_info(fields, department)}]),
	mnesia:create_table(project,[{attributes,record_info(fields, project)}]),
   	mnesia:create_table(managed, [{type, set}, 
                                  {attributes, record_info(fields, managed)}]),	
	mnesia:create_table(works_in, [{type, set}, 
                                  {attributes, record_info(fields, works_in)}]),	
	mnesia:create_table(works_for, [{type, bag}, 
                                  {attributes, record_info(fields, works_for)}]),	
	mnesia:create_table(belongs_to, [{type, set}, 
                                  {attributes, record_info(fields, belongs_to)}]).



%% @function insert_emp/3 - a function to insert emoployee records into the database, 
%% @param Emp - the record employee
%% @param DeptId - the department to which the employee belongs
%% @param ProjNames -the list of projects that the employee is participating;
%% @author Nicolae Paladi

insert_emp(Emp, DeptId, ProjNames) ->
    Ename = Emp#employee.name,
    Fun = fun() ->
                  mnesia:write(Emp),
                  WorksIn = #works_in{employee = Ename, department = DeptId},
                  mnesia:write(WorksIn),
                  add_projs(Ename, ProjNames)
          end,
    mnesia:transaction(Fun).


%% @function add_projs/2 - a function to add projects to which an employee contributes
%% @param Emp - the employee name
%% @param ProjNames - the list of projects to which the employee is added;
%% @author Nicolae Paladi

add_projs(Ename, [ProjName|Tail]) ->
    mnesia:write(#works_for{employee = Ename, project = ProjName}),
    add_projs(Ename, Tail);
add_projs(_, []) -> ok.


%% @function add_dept/3 - a function to add projects to which an employee contributes
%% @param Dept -- the department record
%% @param Empployees - the employees record
%% @param Manager - the record of the department's manager
%% @param ProjNames - the list of projects to which the employee is added;
%% @author Nicolae Paladi

add_dept(Dept, Manager, Employees, ProjNames) ->
    DeptName = Dept#department.name,
    Mang = Manager#employee.name,
    Fun = fun() ->
                  mnesia:write(DeptName),
		  Managed = #managed{employee = Mang, department = DeptName},
                  mnesia:write(Managed),
                  insert_emp(Employees, DeptName, ProjNames)
          end,
    mnesia:transaction(Fun).

%% @function delete/2 - a function to delete entries in the database
%% @param Table -- the name of the table from which entries should be deleted
%% @param Key -- the key of the entries to be deleted
%% @author Nicolae Paladi

delete(Table, Key) ->
	Fun = fun() ->
		mnesia:delete({Table, Key})
	end,
    mnesia:transaction(Fun).		


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Supplementary functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fillDB() ->
       Emp  = #employee{ name = klacke,
                         id = 2423423, 
			salary = 1000},
	Emp2 = #employee{name = robert, 
			id = 232323, 
			salary = 1000},
	Emp3 =  #employee{name = john, 
			id = 232325, 
			salary = 1000},

insert_emp(Emp, 'B/SFR', ['Erlang', mnesia, otp]),
insert_emp(Emp2, 'B/SR', ['Erlang', mnesia, otp]),
insert_emp(Emp3, 'B/SF', ['Erlang', mnesia, otp]).


raise(Eno, Raise) ->
    F = fun() ->
                [E] = mnesia:read(employee, Eno, write),
                Salary = E#employee.salary + Raise,
                New = E#employee{salary = Salary},
                mnesia:write(New)
        end,
    mnesia:transaction(F).

all_sall() ->
    F = fun() ->
                Sall = #employee{salary = 1000, name = '$1', _ = '_'},
                mnesia:select(employee, [{Sall, [], ['$1']}])
        end,
    mnesia:transaction(F).

